package cn.iocoder.yudao.module.forward.service.ack;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.forward.controller.admin.ack.vo.*;
import cn.iocoder.yudao.module.forward.dal.dataobject.ack.Ack1DO;
import cn.iocoder.yudao.module.forward.dal.dataobject.ack.Ack2DO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.forward.dal.mysql.ack.Ack1Mapper;
import cn.iocoder.yudao.module.forward.dal.mysql.ack.Ack2Mapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.forward.enums.ErrorCodeConstants.*;

/**
 * 应答信息 Service 实现类
 *
 * @author zhang
 */
@Service
@Validated
public class Ack1ServiceImpl implements Ack1Service {

    @Resource
    private Ack1Mapper ack1Mapper;
    @Resource
    private Ack2Mapper ack2Mapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer createAck1(Ack1SaveReqVO createReqVO) {
        // 插入
        Ack1DO ack1 = BeanUtils.toBean(createReqVO, Ack1DO.class);
        ack1Mapper.insert(ack1);

        // 插入子表
        createAck2List(ack1.getId(), createReqVO.getAck2s());
        // 返回
        return ack1.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateAck1(Ack1SaveReqVO updateReqVO) {
        // 校验存在
        validateAck1Exists(updateReqVO.getId());
        // 更新
        Ack1DO updateObj = BeanUtils.toBean(updateReqVO, Ack1DO.class);
        ack1Mapper.updateById(updateObj);

        // 更新子表
        updateAck2List(updateReqVO.getId(), updateReqVO.getAck2s());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAck1(Integer id) {
        // 校验存在
        validateAck1Exists(id);
        // 删除
        ack1Mapper.deleteById(id);

        // 删除子表
        deleteAck2ByAck1Id(id);
    }

    private void validateAck1Exists(Integer id) {
        if (ack1Mapper.selectById(id) == null) {
            throw exception(ACK1_NOT_EXISTS);
        }
    }

    @Override
    public Ack1DO getAck1(Integer id) {
        return ack1Mapper.selectById(id);
    }

    @Override
    public PageResult<Ack1DO> getAck1Page(Ack1PageReqVO pageReqVO) {
        return ack1Mapper.selectPage(pageReqVO);
    }

    // ==================== 子表（应答统计） ====================

    @Override
    public List<Ack2DO> getAck2ListByAck1Id(Integer ack1Id) {
        return ack2Mapper.selectListByAck1Id(ack1Id);
    }

    private void createAck2List(Integer ack1Id, List<Ack2DO> list) {
        list.forEach(o -> o.setAck1Id(ack1Id));
        ack2Mapper.insertBatch(list);
    }

    private void updateAck2List(Integer ack1Id, List<Ack2DO> list) {
        deleteAck2ByAck1Id(ack1Id);
		list.forEach(o -> o.setId(null).setUpdater(null).setUpdateTime(null)); // 解决更新情况下：1）id 冲突；2）updateTime 不更新
        createAck2List(ack1Id, list);
    }

    private void deleteAck2ByAck1Id(Integer ack1Id) {
        ack2Mapper.deleteByAck1Id(ack1Id);
    }

}